﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


using Microsoft.Win32;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows;
using System.IO;

namespace app
{
    public class ImageFileServices : INotifyPropertyChanged
    {
        private ImageSource imgSource;

        public ImageFileServices()
        {
        }

        internal void initImg()
        {
            imgSource = new BitmapImage(new Uri("../resource/image/Hydrangeas.jpg", UriKind.RelativeOrAbsolute));
        }

        internal void imageLoadToFile()
        {
            OpenFileDialog m_OpenFile = new OpenFileDialog();

            m_OpenFile.DefaultExt = "jpg";
            m_OpenFile.Filter = "Graphics interchange Format (*.jpg)|*.jpg|All files(*.*)|*.*";
            m_OpenFile.ShowDialog();

            if (m_OpenFile.FileName.Length > 0)
            {
                BitmapImage m_MyBitmapImage = new BitmapImage();

                m_MyBitmapImage.BeginInit();
                m_MyBitmapImage.UriSource = new Uri(@m_OpenFile.FileName);
                m_MyBitmapImage.EndInit();
                ImgSource = m_MyBitmapImage;        // imgSource로 접근했다(x) 소문자는 변수이고, 메서드로 접근하려면 대문자로!! 주의하자! ImgSource로!
            }
        }

        internal RenderTargetBitmap imageRender(Image img)
        {
            //imgSource = new BitmapImage(new Uri("../resource/image/Desert.jpg", UriKind.RelativeOrAbsolute));
            Rect bounds = VisualTreeHelper.GetDescendantBounds(img);
            RenderTargetBitmap bmp = new RenderTargetBitmap((int)img.ActualWidth / 1,
                                    (int)img.ActualHeight / 1, 1 / 96, 1 / 96, PixelFormats.Pbgra32);
            DrawingVisual dv = new DrawingVisual();
            using (DrawingContext ctx = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(img);
                ctx.DrawRectangle(vb, null, new Rect(new System.Windows.Point(0, 0), bounds.Size));
            }

            bmp.Render(dv);
            return bmp;
        }

        internal void SaveBitmapToFile(RenderTargetBitmap bmp)
        {
            string fileName = "temp" + new Random().Next(1000) + ".jpg";
            string path = "CaptureImage/" + fileName;
            FileStream fs = new FileStream(path, FileMode.Create);

            if (Directory.Exists(path))
            {
                MessageBox.Show("경로가 없습니다.");
            }
            else
            {
                MessageBox.Show(path + "로 저장되었습니다.");
            }
            BitmapEncoder encoder = new TiffBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bmp));
            encoder.Save(fs);
            fs.Close();
        }

        public ImageSource ImgSource
        {
            get { return imgSource; }
            set { imgSource = value; OnPropertyChanged("ImgSource"); }
        }
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}
